package com.seveneight.baijiesteward.ui.main.activity;

import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.seveneight.baijiesteward.R;
import com.seveneight.baijiesteward.base.BaseActivity;
import com.seveneight.baijiesteward.bean.UserBean;
import com.seveneight.baijiesteward.http.AppConfig;
import com.seveneight.baijiesteward.ui.main.contract.LoginContract;
import com.seveneight.baijiesteward.ui.main.presenter.LoginPresenter;
import com.seveneight.baijiesteward.utils.LogUtil;
import com.seveneight.baijiesteward.utils.MDialog;
import com.seveneight.baijiesteward.utils.PreferencesUtil;
import com.seveneight.baijiesteward.utils.Setting;
import com.seveneight.baijiesteward.utils.ToastUtil;
import com.seveneight.baijiesteward.utils.WindowUtil;
import com.tencent.bugly.crashreport.CrashReport;

import butterknife.BindView;

/**
 * Created by user1 on 2018/2/11.
 */

public class LoginActivity extends BaseActivity implements View.OnClickListener, LoginContract.LoginView {

    private static int TYPE_CHANGELOGINPWD = 2;
    LoginContract.LoginPresenter presenter;
    @BindView(R.id.et_account)
    EditText et_account;
    @BindView(R.id.et_pwd)
    EditText et_pwd;
    @BindView(R.id.cb_view)
    CheckBox cb_view;
    @BindView(R.id.bt_login)
    Button bt_login;
    @BindView(R.id.tv_forgetpwd)
    TextView tv_forgetpwd;
    @BindView(R.id.tv_register)
    TextView tv_register;

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_login);
    }

    @Override
    protected void initView() {
        setSwipeBackEnable(false);
    }

    @Override
    protected void initData() {
        presenter = new LoginPresenter(this);
    }

    @Override
    protected void setListener() {
        tv_register.setOnClickListener(this);
        bt_login.setOnClickListener(this);
        tv_forgetpwd.setOnClickListener(this);
        et_account.setText(PreferencesUtil.get(getContext(),PreferencesUtil.ACCOUNT,""));
        cb_view.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean flag) {
                if (flag) {
                    et_pwd.setTransformationMethod(null);
                    et_pwd.setSelection(et_pwd.getText().toString().length());
                } else {
                    et_pwd.setTransformationMethod(new PasswordTransformationMethod());
                    et_pwd.setSelection(et_pwd.getText().toString().length());
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_login:
                login();
                break;
            case R.id.tv_forgetpwd:
                Bundle bundle = new Bundle();
                bundle.putInt("type", TYPE_CHANGELOGINPWD);
                WindowUtil.getInstance().startActivity(activity, ForgetPwdActivity.class, bundle);
                break;
            case R.id.tv_register:
                Bundle bundle2 = new Bundle();
                bundle2.putString("url", AppConfig.APP_REGISTER);
                WindowUtil.getInstance().startActivity(activity, WebActivity.class, bundle2);
                break;
        }
    }

    private void login() {
        String mobile = et_account.getText().toString().trim();
        if (TextUtils.isEmpty(mobile)) {
            showDialog("请输入手机号");
            return;
        }

        String pwd = et_pwd.getText().toString().trim();
        if (TextUtils.isEmpty(pwd)) {
            showDialog("请输入密码");
            return;
        }
        PreferencesUtil.put(getContext(),PreferencesUtil.ACCOUNT,mobile);
        presenter.login(activity, jsonFormat.login(mobile, pwd, null));
    }

    @Override
    public void finish() {
        super.finish();
        this.overridePendingTransition(0, R.anim.activity_close);
    }

    @Override
    public void reBackkUser(UserBean bean) {
        presenter.getUserInfo("正在获取个人信息...", activity, jsonFormat.getUserInfo(bean.getUserID()));
        PreferencesUtil.put(getContext(),PreferencesUtil.userId, bean.getUserID());
    }

    @Override
    public void backUserInfo(UserBean bean) {
        LogUtil.d(bean.toString());
        setting.setUserInfo(bean);
        afterFinish();
    }
}
